package com.uchiha.nova.core.tool.convert;

import org.springframework.boot.convert.ApplicationConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.lang.Nullable;
import org.springframework.util.StringValueResolver;

/**
 * 类型 转换 服务，添加了 IEnum 转换
 * @author 周林静
 * @Date 2021/4/29 15:52
 * @Version 1.0
 */
public class NovaConversionService extends ApplicationConversionService {
    @Nullable
    private static volatile NovaConversionService SHARED_INSTANCE;

    public NovaConversionService() {
        this(null);
    }

    public NovaConversionService(@Nullable StringValueResolver embeddedValueResolver) {
        super(embeddedValueResolver);
        super.addConverter(new EnumToStringConverter());
        super.addConverter(new StringToEnumConverter());
    }

    /**
     * Return a shared default application {@code ConversionService} instance, lazily
     * building it once needed.
     * <p>
     * Note: This method actually returns an {@link NovaConversionService}
     * instance. However, the {@code ConversionService} signature has been preserved for
     * binary compatibility.
     * @return the shared {@code BladeConversionService} instance (never{@code null})
     */
    public static GenericConversionService getInstance() {
        NovaConversionService sharedInstance = NovaConversionService.SHARED_INSTANCE;
        if (sharedInstance == null) {
            synchronized (NovaConversionService.class) {
                sharedInstance = NovaConversionService.SHARED_INSTANCE;
                if (sharedInstance == null) {
                    sharedInstance = new NovaConversionService();
                    NovaConversionService.SHARED_INSTANCE = sharedInstance;
                }
            }
        }
        return sharedInstance;
    }

}
